package com.hsnn.medstgmini.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.dict.model.DicArea;
import com.hsnn.medstgmini.base.dict.model.DicDrugform;
import com.hsnn.medstgmini.base.dict.model.DicDrugmaterial;
import com.hsnn.medstgmini.base.dict.model.DicEntProfile;
import com.hsnn.medstgmini.base.dict.model.DicExperttype;
import com.hsnn.medstgmini.base.dict.model.DicPhar;
import com.hsnn.medstgmini.base.dict.model.DicSys;
import com.hsnn.medstgmini.dao.DynamicSelectDao;
import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.pro.project.model.BidProject;
import com.hsnn.medstgmini.util.FormatDate;


@Repository("selectDao")
public class DynamicSelectDaoImpl implements DynamicSelectDao {

	
	
	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;
	@Autowired
	private HibernateSqlUtil dao;
	
//	public SessionFactory getSessionFactory() {
//        return this.sessionFactory;
//    }
//	 public Session getSession() throws HibernateException {
//	        Session sess = getSessionFactory().getCurrentSession();
//	        if (sess == null) {
//	            sess = getSessionFactory().openSession();
//	        }
//	        return sess;
//	    }
//	 @Autowired
//	    @Required
//	    public void setSessionFactory(SessionFactory sessionFactory) {
//	        this.sessionFactory = sessionFactory;
//	    }

	@SuppressWarnings("unchecked")
	@Override
	public List<DicSys> getOrderStatus(String classifyCode) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(DicSys.class);
		c.add(Restrictions.eq("classifyCode", classifyCode));
		return c.list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<DicSys> getArchiveType(String classifyCode) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(DicSys.class);
		c.add(Restrictions.eq("classifyCode", classifyCode))
		.add(Restrictions.eq("status", (short)1));
		return c.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<DicArea> getArea(String fatherId){
		Criteria c = sessionFactory.getCurrentSession().createCriteria(DicArea.class);
		c.add(Restrictions.eq("father.areaid", fatherId))
		.add(Restrictions.eq("status", 1));
		return c.list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<DicArea> getQYArea(String fatherId){
		Criteria c = sessionFactory.getCurrentSession().createCriteria(DicArea.class);
		c.add(Restrictions.eq("father.areaid", fatherId))
		.add(Restrictions.eq("status", 1));
		return c.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<DicPhar> getPhar(String fatherId){
		Criteria c = sessionFactory.getCurrentSession().createCriteria(DicPhar.class);
		c.add(Restrictions.eq("fatherId", fatherId))
		.add(Restrictions.eq("status", 1));
		return c.list();
	}
	@SuppressWarnings("unchecked")
	public List<Object> getForm(String fatherId){
		/*Criteria c = sessionFactory.getCurrentSession().createCriteria(DicDrugform.class);
		c.add(Restrictions.eq("father.formId", fatherId));*/
		String sql = "select form_id,form_name from dic_drugform where father_id=:fatherId and status=1";
		Query query=sessionFactory.getCurrentSession().createSQLQuery(sql);
		query.setString("fatherId", fatherId);
		return query.list();
	}
	
	public DicDrugform getFormById(String formid){
		DicDrugform data = (DicDrugform)sessionFactory.getCurrentSession().get(DicDrugform.class, formid);
		return data;
	}
	
	@SuppressWarnings("unchecked")
	public List<DicDrugmaterial> getMaterial(String fatherId){
		Criteria c = sessionFactory.getCurrentSession().createCriteria(DicDrugmaterial.class);
		c.add(Restrictions.eq("status", Short.valueOf("1")));
		return c.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<DicSys> getDiSys(String classifyCode){
		String hql = "from DicSys t where t.classifyCode=:classifyCode and t.status='1' order by t.dicCode+0 asc";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString("classifyCode", classifyCode);
		List<DicSys> list = query.list();
		return list;
	}
	

	public List <BidProject> getProject (String ID){
		String hql = "from BidProject t where t.status=1 and t.starttime<:starttime and t.endtime >:endtime";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString("starttime", FormatDate.toFullFormat(new Date()));
		query.setString("endtime", FormatDate.toFullFormat(new Date()));
		List<BidProject> list = query.list();
		return list;
	}
	public List<Object[]> getMyProj(String hospCode){
		StringBuffer sql = new StringBuffer()
		.append("SELECT distinct a.proj_id,a.proj_name name from base_goods_item a inner join pur_hospcatalog b on b.goods_code=a.goods_code ")
		.append(" where a.status=1 and a.proj_status=1 and SYSDATE() between a.proj_starttime and a.proj_endtime ")
		.append(" and b.hosp_code=:hospCode");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("hospCode", hospCode);
		return (List<Object[]>) dao.getSqlList(sql.toString(), params);
	}
	@Override
	public List<Object[]> getCompProj(String compId) {
		StringBuffer sql = new StringBuffer()
		.append("SELECT a.proj_id,b.name from bid_projcompany a left join bid_project b on a.proj_id=b.proj_id ")
		.append(" where a.status=1 and b.status=1 and b.starttime<= SYSDATE() and b.endtime>=SYSDATE()")
		.append(" and a.comp_id=:hospId");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("hospId", compId);
		return (List<Object[]>) dao.getSqlList(sql.toString(), params);
	}
	
	@Override
	public List<Object[]> getProjByProdCompId(String proCompId) {
		StringBuffer sql = new StringBuffer()
		.append(" SELECT DISTINCT a.proj_id ,")
		.append(" 	a.proj_name name")
		.append(" FROM")
		.append(" 	base_goods_item a")
		.append(" WHERE")
		.append(" 	  a.STATUS = 1")
		.append(" AND a.qualityspec_status = 1")
		.append(" AND a.proj_status = 1")
		.append(" AND SYSDATE() BETWEEN a.proj_starttime and a.proj_endtime")
		.append(" AND a.prod_compcode=:compCode");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("compCode", proCompId);
		return (List<Object[]>) dao.getSqlList(sql.toString(), params);
	}
	
	public List<Object[]> getProj(){
		StringBuffer sql = new StringBuffer()
		.append("SELECT b.proj_id,b.name from bid_project b ")
		.append(" where  b.status=1 and b.starttime<= SYSDATE() and b.endtime>=SYSDATE()");
//		.append(" and a.comp_id=:hospId");
		Map<String, Object> params = new HashMap<String, Object>();
		return (List<Object[]>) dao.getSqlList(sql.toString(), params);
	}

	@SuppressWarnings("unchecked")
	public List<DicExperttype> getExpertType(String fatherId,int status) {
		Session session = sessionFactory.getCurrentSession();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from dic_experttype where father_experttype_id = :fatherId ");
		if(status==Constants.ENABLED){
			sql.append(" and status=:status");
		}
		SQLQuery sqlquery = session.createSQLQuery(sql.toString());
		sqlquery.addEntity(DicExperttype.class);
		sqlquery.setParameter("fatherId", fatherId);
		if(status==Constants.ENABLED){
			sqlquery.setParameter("status", status);
		}
		return sqlquery.list();
	}

	/**
	 * 
	 * @category 获取企业资质类型
	 * @author wangbing
	 * @date 2015年6月10日 下午7:06:29
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<DicEntProfile> getProfileType() {
		Session session = sessionFactory.getCurrentSession();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from dic_ent_profile ");
		SQLQuery sqlquery = session.createSQLQuery(sql.toString());
		sqlquery.addEntity(DicEntProfile.class);
		return sqlquery.list();
	}
	
}
